<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class backup_database extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		//$this->load->model('mdl_master_akun', 'master_akun');
		
	}
	
	function index()
	{
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		
		# config pagination
		$config['base_url'] = base_url().'index.php/backup_database/index/'; 
		
		# load view
		$this->load->view('backup_database/backup_database_export', $data);
		
		$this->close();
	}
	
	function import_page()	{				$data['can_view'] 	= $this->can_view();		$data['can_insert'] = $this->can_insert();		$data['can_update'] = $this->can_update();		$data['can_delete'] = $this->can_delete();				$this->open();				# config pagination		$config['base_url'] = base_url().'index.php/backup_database/index/'; 				# load view		$this->load->view('backup_database/backup_database_import', $data);				$this->close();	} 			function export()	{		 		//$this->load->view('backup_database/export' );		$this->backup_tables('localhost','root','','bp3ti');	}		function import()	{ 		//$this->load->view('backup_database/import' );		// <?php		$filename	=$_FILES['importfile']['name'];		$tmpname	=$_FILES['importfile']['tmp_name'];		$pat=$filename;		if(@copy($tmpname, $pat)) 		{			?>		<script type='text/javascript'>alert('Import Sukses!');		window.location.href='<?=base_url();?>/index.php/backup_database/';		</script>";		<?				}		else		{		echo "<script type='text/javascript'>alert('Gagal di import!');		window.history.back(-1);		</script>";		}		//include ("ClassSQLimport.php");		require_once APPPATH.'libraries/MySQL_import.php';		$check = false;		$database = 'bp3ti';		$host = "localhost";		$dbUser = "root";		$dbPassword = "";		$sqlFile = $filename;		$newImport = new sqlImport($sqlFile, $check, $host, $dbUser, $dbPassword, $database);		$newImport -> import ();		@unlink($pat);			}	/* backup the db OR just a table */	function backup_tables($host,$user,$pass,$name,$tables = '*')	{                $link = mysql_connect($host,$user,$pass);		 $return = '';        mysql_select_db($name,$link);                //get all of the tables        if($tables == '*')        {                $tables = array();                $result = mysql_query('SHOW TABLES');                while($row = mysql_fetch_row($result))                {                        $tables[] = $row[0];                }        }        else        {                $tables = is_array($tables) ? $tables : explode(',',$tables);        }                //cycle through        foreach($tables as $table)        {				                 $result = mysql_query('SELECT * FROM '.$table);                $num_fields = mysql_num_fields($result);                               $return.= 'DROP TABLE IF EXISTS '.$table.';';                $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));                $return.= "\n\n".$row2[1].";\n\n";                                for ($i = 0; $i < $num_fields; $i++)                 {                        while($row = mysql_fetch_row($result))                        {                                $return.= 'INSERT INTO '.$table.' VALUES(';                                for($j=0; $j<$num_fields; $j++)                                 {                                        $row[$j] = addslashes($row[$j]);                                        $row[$j] = ereg_replace("\n","\\n",$row[$j]);                                        if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }                                        if ($j<($num_fields-1)) { $return.= ','; }                                }                                $return.= ");\n";                        }                }                $return.="\n\n\n";        }        			//save file						//echo $return;		   // $handle = fopen('db-backup2-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');		   // fwrite($handle,$return);		   // fclose($handle);				$tz_string = "Asia/Jakarta"; // Use one from list of TZ names http://php.net/manual/en/timezones.php 				$tz_object = new DateTimeZone($tz_string); 								$datetime = new DateTime(); 				$datetime->setTimezone($tz_object); 		header("Content-Disposition: attachment; filename=database SIMKEU BP3TI - ".$datetime->format('Y.m.d')."-".$datetime->format('H.i.s').".sql");		header("Content-type: application/download");						   echo $return;	}	 		 
}